﻿using System;
using PhoneCore.Framework;
using PhoneCore.Framework.Bootstrap;
using PhoneCore.Framework.Configuration;
using PhoneCore.Framework.Diagnostic.Tracing;
using PhoneCore.Framework.Diagnostic.Tracing.Data;
using PhoneCore.Framework.IoC;
using SecureBox.UI.Infrastructure.Data;
using Template = SecureBox.UI.Infrastructure.Model.Template;

namespace SecureBox.UI.Infrastructure.Plugins
{
    /// <summary>
    /// Initializes DataContextService
    /// </summary>
    public class DataContextPlugin : BootstrapperPlugin
    {
        public DataContextPlugin(IConfigSection config) : base(config) { }

        public override bool Run()
        {
            try
            {
                Trace.Info(Category, "register data context instance");

                //get connection string
                string connectionString =
                    ConfigSettings.Instance.GetSection("data/storage").GetString("@connectionString");

                //create DataContext instance
                SecureBoxDataContext dataContext = new SecureBoxDataContext(connectionString);
                //register data context service
                Container.RegisterType<IDataContextService, DataContextService>(dataContext);
                //check whether database exists
                if(dataContext.DatabaseExists())
                    dataContext.DeleteDatabase();

                if (!dataContext.DatabaseExists())
                {
                   //  dataContext.DeleteDatabase();
                   // }
                    CreateDatabase(dataContext);
                }

                return true;
            }
            catch (Exception ex)
            {
                Trace.Fatal(Category, "run is failed", ex);
                throw;
            }
        }

        /// <summary>
        /// The task isn't updatable
        /// </summary>
        /// <returns></returns>
        public override bool Update()
        {
            Trace.Info(Category, "update");
            return false;
        }

        /// <summary>
        /// Creates dataBase
        /// </summary>
        /// <param name="dataContext"></param>
        private void CreateDatabase(SecureBoxDataContext dataContext)
        {
            Trace.Info(Category, "create database");
            dataContext.CreateDatabase();

            IDataContextService dataContextService = Container.Resolve<IDataContextService>();

            Trace.Info(Category, "insert templates from configuration");
            var templates = ConfigSettings.Instance.GetSections("data/storage/init/templates/template");
            foreach (var templateConfig in templates)
            {
                var name = templateConfig.GetString("@name");
                var imageUrl = templateConfig.GetString("@imageUrl");
                dataContextService.Templates.Add(new Template() {Name = name, ImageUrl = imageUrl});
            }
            dataContextService.Commit();
        }

    }
}
